package com.sks.web.action;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.util.CollectionUtils;

import sun.util.logging.resources.logging;

import com.sks.bean.pojo.House;
import com.sks.bean.pojo.Users;
import com.sks.bean.uploadfile.UploadFile;
import com.sks.core.SystemConstants;
import com.sks.utils.WebUtil;

public class BuildHtmlFile {
	
	private static String houseHistoryHtml = 
		"<tr id=\"#houseId#\"><td width=\"5%\" height=\"80px;\"></td>\n" +
		"<td width=\"40%\" valign=\"middle\">\n" +
			"<a href=\"#basePath#sks/house.do?method=info&houseId=#houseId#\" target=\"_blank\">\n" +
			"<img src=\"#basePath##houseImg#\" height=\"60px\" width=\"95px\" /></a></td>\n" +
		"<td valign=\"middle\"><a href=\"#basePath#sks/house.do?method=info&houseId=#houseId#\" target=\"_blank\">#buildingName#</a><br>\n" +
			"<span>[#houseArea#]#houseSpace#㎡</span><br>\n" +
			"<span style=\"color: #eb6100; font-weight: bold;\">#housePrice#</span><br></td>\n" +
		"</tr>\n";
	
	public static void createHouseHtml(House house, HttpServletRequest request){
		/*File saveDir = new File(request.getSession().getServletContext().getRealPath("/"+SystemConstants.SKS_HOUSE_HTML_PATH));
		File saveDirBuy = new File(request.getSession().getServletContext().getRealPath("/"+SystemConstants.SKS_HOUSE_HTML_PATH_BUG));
		try {
			if(!saveDir.exists()) {
				saveDir.mkdirs();
			}
			VelocityContext context = new VelocityContext();
			context.put("house", house);
			String path = request.getContextPath(); 
			String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
			context.put("basePath", basePath);
			context.put("housePath", SystemConstants.SKS_HOUSE_HTML_PATH);
			
			//boolean isCollected = request.getAttribute("isCollected") != null ? (Boolean) request.getAttribute("isCollected") : false;
			//context.put("isCollected", isCollected);
			List<UploadFile> pictures = house.getPictures();
			context.put("firstPic", pictures.iterator().next());
			context.put("pictures", pictures);
			List<UploadFile> videos = house.getVideos();
			context.put("videos", videos);
			List<UploadFile> buildingPictures = house.getBuilding().getPictures();
			context.put("buildingfirstPic", buildingPictures.iterator().next());
			context.put("buildingPictures", buildingPictures);
			context.put("comments", house.getComments());
			
			Template template = Velocity.getTemplate("house/houseInfoTemp.html");
			
			if ( house.getTransaStatus() == 2 || house.getTransaStatus() == 0) {
				FileOutputStream outStream = new FileOutputStream(new File(saveDir, house.getHouseId()+".shtml"));
				OutputStreamWriter writer =  new OutputStreamWriter(outStream,"UTF-8");
				BufferedWriter sw = new BufferedWriter(writer);
				String menuName = "houseListRent";
				context.put("isRent", true);
				context.put("menuName", menuName);
				template.merge(context, sw);
				sw.flush();
				sw.close();
				outStream.close();
			}
			if ( house.getTransaStatus() == 2 || house.getTransaStatus() == 1) {
				FileOutputStream outStreamBuy = new FileOutputStream(new File(saveDirBuy, house.getHouseId()+".shtml"));
				OutputStreamWriter writerBuy =  new OutputStreamWriter(outStreamBuy,"UTF-8");
				BufferedWriter swBuy = new BufferedWriter(writerBuy);
				String menuName = "houseListBuy";
				context.put("isRent", false);
				context.put("menuName", menuName);
				template.merge(context, swBuy);
				swBuy.flush();
				swBuy.close();
				outStreamBuy.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	}
	
	public static String buildHouseHisotry(HttpServletRequest request) {
		List<House> houses = WebUtil.getHouseHistory(request);
		StringBuffer sb = new StringBuffer();
		if (CollectionUtils.isEmpty(houses)) {
			return "";
		}
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		try {
			
			// 限制只显示最新浏览的5个房源
			int limit = houses.size() >= 5 ? houses.size() - 5 : 0; 
			for (int i = houses.size() - 1 ; i >= 0 && i >= limit ; i--) {
				House house = houses.get(i);
				String temp = houseHistoryHtml;
				temp = temp.replaceAll("#houseId#", house.getHouseId() + "");
				temp = temp.replaceAll("#basePath#", basePath);
				
				List<UploadFile> pics = house.getPictures();
				if (CollectionUtils.isEmpty(pics)) {
					temp = temp.replaceAll("#houseImg#", "sks/img/frontpages/lack_house_b.png");
				} else {
					temp = temp.replaceAll("#houseImg#", pics.get(0).getFullPathName());
				}
				
				temp = temp.replaceAll("#buildingName#", (null == house.getBuilding() ? "" : house.getBuilding().getName()));
				temp = temp.replaceAll("#houseArea#", (null == house.getArea() ? "" : house.getArea().getName()));
				temp = temp.replaceAll("#houseSpace#", house.getSpace() + "");
				
				if (house.getTransaStatus() == 1) {
					temp = temp.replaceAll("#housePrice#", house.getSalePrice() + "/平方米");
				} else {
					temp = temp.replaceAll("#housePrice#", house.getRentPrice() + "/月");
				}
				
				sb.append(temp);
			}
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
}
